﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace Crosshatch
{
    public partial class GamePage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;

        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            DotManager.LoadContent(contentManager);
            DotManager.Reset();

            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            if (DotManager.health > 0)
            {
                DotManager.Update(e);
                DotManager.TakeInput(TouchPanel.GetState());
            }
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);

            for (int x = 0; x < Global.ScreenWidth / Global.gridSize + 1; x++)
            {
                spriteBatch.Draw(
                    Global.white,
                    new Rectangle(
                        x * Global.gridSize - 2,
                        0,
                        2,
                        Global.ScreenHeight),
                    Color.Gray);
            }
            for (int y = 0; y < Global.ScreenHeight / Global.gridSize + 1; y++)
            {
                spriteBatch.Draw(
                    Global.white,
                    new Rectangle(
                        0,
                        y * Global.gridSize - 2,
                        Global.ScreenWidth,
                        2),
                    Color.Gray);
            }

            DotManager.Draw(spriteBatch);

            //Gameover
            if (DotManager.health <= 0)
            {
                DotManager.ReportScore(DotManager.score);
                spriteBatch.DrawString(
                    DotManager.ui,
                    "GAME OVER",
                    new Vector2(
                        Global.ScreenWidth / 2 - DotManager.ui.MeasureString("GAME OVER").X / 2,
                        Global.ScreenHeight / 2),
                    Color.White);
                spriteBatch.DrawString(
                    DotManager.subui,
                    "Play Again: Press Back",
                    new Vector2(
                        Global.ScreenWidth / 2 - DotManager.subui.MeasureString("Play Again: Press Back").X / 2,
                        Global.ScreenHeight / 2 + 50),
                    Color.LightGray);
            }

            spriteBatch.End();
        }
    }
}